package com.common.queueloader.items
{
	import com.common.queueloader.AbstractItem;
	import com.common.queueloader.ILoadable;
	import com.common.queueloader.QueueVo;
	import com.common.zip.fzip.CFZip;
	import com.common.zip.fzip.FZip;
	import com.common.zip.fzip.FZipFile;
	
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.events.HTTPStatusEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;

	/**
	 *加载zip文件 (加密过)
	 * @author lake
	 * 
	 */	
	public class CZipItem extends AbstractItem implements ILoadable
	{
		private var fZipLoader :CFZip;
		
		public function CZipItem(queueVo:QueueVo, path:URLRequest, fileType:int=0)
		{
			super(queueVo, path, fileType);
		}
		override public function load() : void 
		{
			fZipLoader = new CFZip();
			fZipLoader.load(path);
			fZipLoader.addEventListener(Event.COMPLETE, onComplete);
			fZipLoader.addEventListener(ProgressEvent.PROGRESS , onProgress);
		}
		
		override public function dispose() : void 
		{
			_progress = 0;
			_bytesLoaded = 0;
			_bytesTotal = 0;
			_queueVo = null;
			_path = null;
			_isLoading = false;
			_fileType = 0;
			_loader = null;
			_httpStatusFunction = null;
			_errorFunction = null;
			_completeFunction = null;
			_progressFunction = null;
		}
		
		override public function close():void
		{
			if(fZipLoader)
			{
				fZipLoader.close();
			}
		}
		
		
		override public function onComplete(evt:Event):void 
		{
			var count:uint = 0;
			var filesList :Array = [];
			var itemObj :Object;
			for(var i:int=0; i<fZipLoader.getFileCount(); i++)
			{
				var file:FZipFile = fZipLoader.getFileAt(i);
				itemObj = new Object();
				itemObj.name = file.filename.split(".")[0];
				itemObj.content = file.content;
				filesList.push(itemObj);
			}
			_content = filesList;
			_completeFunction(evt);
			fZipLoader.removeEventListener(Event.COMPLETE, onComplete);
			fZipLoader.removeEventListener(ProgressEvent.PROGRESS , onProgress);
			this.dispose();
		}
		override public function onProgress(evt:ProgressEvent):void
		{
			if(_progressFunction==null)return;
			_bytesLoaded = evt.bytesLoaded;
			_bytesTotal = evt.bytesTotal;
			_progress = Math.floor(evt.bytesLoaded / evt.bytesTotal * 100 );
			_progressFunction(evt.clone());
		}
		override public function onHttpStatus(evt:HTTPStatusEvent):void
		{
			
		}
		override public function onError(evt:ErrorEvent):void
		{
			
		}
	}
}